// 导入axios
import axios from "axios";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
//创建实例
const instance = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
    // http://1.14.239.98/backend/api/v1/
    timeout: 10000,
});

// 配置拦截器
// 请求拦截器
instance.interceptors.request.use(
    (config) => {
        NProgress.start();
        const token = localStorage.getItem('admin-token');
        if (token) {
            config.headers["Authorization"] = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        NProgress.done();
        return Promise.reject(error)
    })

// 响应拦截器
instance.interceptors.response.use(
    (response) => {
        // 以服务的方式调用的 Loading 需要异步关闭
        NProgress.done();
        return response.data;
    },
    (error) => {
        NProgress.done();
        return Promise.reject(error)
    });

// 导出配置
export default instance;